package com.zjh.photoshopstudy.app.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zjh.photoshopstudy.Model.VideoEntity;
import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.app.video.VideoActivity;

import java.util.List;

import static com.zjh.photoshopstudy.Utils.Data.LEVELS;
import static com.zjh.photoshopstudy.Utils.Data.webRoot_URL;

public class MainListAdapter extends RecyclerView.Adapter<MainListAdapter.MainView> {
    private String[] types = {"全部", "理论基础", "字体设计", "修图课程", "鼠绘手绘", "海报设计", "特效技巧", "文字排版"};
    private List<VideoEntity> mVideoEntityList;
    private Context mContext;

    public MainListAdapter(List<VideoEntity> list, Context context) {
        this.mVideoEntityList = list;
        this.mContext = context;
    }

    @NonNull
    @Override
    public MainView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_main_adapter, parent, false);
        v.getLayoutParams().height = (int) (parent.getWidth() / 2);
        return new MainView(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MainView holder, final int position) {
        holder.mTitle.setText(mVideoEntityList.get(position).getvTitle());
        holder.mType.setText(types[mVideoEntityList.get(position).getvType()]);
        holder.mLevel.setText(LEVELS[mVideoEntityList.get(position).getvLevel()]);
        Glide.with(mContext).load(webRoot_URL + mVideoEntityList.get(position).getvThumbnailPath()).into(holder.mImage);

        holder.mImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, VideoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("videoEntity", mVideoEntityList.get(position));
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mVideoEntityList.size();
    }

    public class MainView extends RecyclerView.ViewHolder {
        ImageView mImage;
        TextView mTitle;
        TextView mType;
        TextView mLevel;


        public MainView(@NonNull View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.image);
            mTitle = itemView.findViewById(R.id.title);
            mType = itemView.findViewById(R.id.type);
            mLevel = itemView.findViewById(R.id.level);
        }
    }
}
